package sisi.menu;

import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.zkoss.lang.Library;
import org.zkoss.web.Attributes;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.ConventionWires;
import org.zkoss.zul.Button;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Tab;
import org.zkoss.zul.Tabpanel;
import org.zkoss.zul.Window;


/**
 * MenuWindow.java - Controller class for the <i>menu</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 23/09/2013
 * 
 */ 
@SuppressWarnings("serial")
public class MenuWindow extends Window implements
		org.zkoss.zk.ui.ext.AfterCompose {

	private Listbox menuDirectoriesListbox, menuProductsListbox, menuServicesListbox, menuEnquiresListbox, menuAdministrationListbox;
	protected Map<String, Object> args;
	private Window finestraMenu;
	private Tab tabAdministration;
	private Tabpanel tabpanelAdministration;
	private Button btnLogout;
	private Label lblUser;

	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void onCreate$finestraMenu(Event event) {
		
		
		Principal principal = Executions.getCurrent().getUserPrincipal();
		String user = principal.getName();
		HttpServletRequest request = (HttpServletRequest) Executions.getCurrent().getNativeRequest();
		principal = request.getUserPrincipal();
		
		boolean isAdmin = request.isUserInRole("AdminPico");
		
		if (!isAdmin){ 
			tabpanelAdministration.setVisible(false);
			menuAdministrationListbox.setVisible(false);
			tabAdministration.setVisible(false);
		}
		
		Library.setProperty(Attributes.PREFERRED_LOCALE, "it");
		
		finestraMenu.setTitle("PICO v.2 - National Fueling");
		btnLogout.setLabel("Logout");
		lblUser.setValue("User: "+user); 
		
		menuDirectoriesListbox.addEventListener("onDoubleClick", new EventListener() {
			public void onEvent(Event event) {
				int rowSelected = menuDirectoriesListbox.getSelectedIndex();
				directoriesMenu(rowSelected); 
			}
		});
		
		menuProductsListbox.addEventListener("onDoubleClick", new EventListener() {
			public void onEvent(Event event) {
				int rowSelected = menuProductsListbox.getSelectedIndex();
				productsMenu(rowSelected);
			}
		});
		
		menuServicesListbox.addEventListener("onDoubleClick", new EventListener() {
			public void onEvent(Event event) {
				int rowSelected = menuServicesListbox.getSelectedIndex();
				servicesMenu(rowSelected);
			}
		});

		
		menuEnquiresListbox.addEventListener("onDoubleClick", new EventListener() {
			public void onEvent(Event event) {
				int rowSelected = menuEnquiresListbox.getSelectedIndex();
				enquiresMenu(rowSelected);
			}
		}); 


		if (isAdmin){ 
			menuAdministrationListbox.addEventListener("onDoubleClick", new EventListener() {
				public void onEvent(Event event) {
					int rowSelected = menuAdministrationListbox.getSelectedIndex();
					administrationMenu(rowSelected);
				}
			});
		}

		
	}

	
	public void afterCompose() {

		// wire variables
		ConventionWires.wireVariables(this, this);

		// auto forward
		ConventionWires.addForwards(this, this);

	}

	
	public void onClick$btnLogout(){
		Sessions.getCurrent().invalidate();
		Executions.sendRedirect("/");
	}
	
	private void directoriesMenu(int rowSelected){
		switch (rowSelected) {
		case 0:
			agentsList();
			break;
		case 1:
			banksList();
			break;
		case 2:
			 brokerList();
			break;
		case 3:
			categoriesList();
			break;
		case 4:
			corporationList();
			break;
		case 5:
			currencyList();
			break;
		case 6:
			paymentsModeList();
			break;
		case 7:
			paymentsTermsList();
			break;
		case 8:
			portList();
			break;
		case 9:
			productsList();
			break;
		case 10:
			captainsList();
			break;
		case 11:
			yachtList();
			break;
		case 12:
			suppliersList();
			break;
		case 13:
			unitMeasureList();
		case 14:
			vatArticleList();	
		
		default:
			break;
			
		}
	}

	private void productsMenu(int rowSelected){
		switch (rowSelected) {
		case 0:
			productsList();
			break;
		case 1:
			categoriesList();
			break;
		
		default:
			break;
		}
	}
	
	private void servicesMenu(int rowSelected){
		switch (rowSelected) {
		case 0:
			currencyList();
			break;
	
		default:
			break;
		}
	}
	
	private void enquiresMenu(int rowSelected){
		switch (rowSelected) {
		case 0:
			customersEnquires();
			break;
		case 1:
			invitationsToQuote();
			break;
		case 2:
			quoteToCustomer();
			break;
		case 3:
			acceptanceQuote();
			break;
		
		default:
			break;
		}
	}
	
	private void administrationMenu(int rowSelected){
		switch (rowSelected) {
		case 0:
			invoice(1); 
			break;
			
		case 1:
			invoice(5);
			break;
		case 2:
			dayBook();
			break;
		case 3:
			customersCard();
			break;
		case 4:
			suppliersCard();
			break;
			
		default:
			break;
		}
	}
	
	// directories
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void yachtList(){
		HashMap map = new HashMap();
		map.put("tastoSeleziona", false);
		Window finestraBrowseYachts = (Window) Executions.createComponents(
				"/yachts.zul", null, map);
		finestraBrowseYachts.doModal();

	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void corporationList(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraBrowseYachts = (Window) Executions.createComponents(
				"/corporations.zul", null, map);
		finestraBrowseYachts.doModal();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void suppliersList(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraBrowseSupplier = (Window) Executions.createComponents(
				"/supplier.zul", null, map);
		finestraBrowseSupplier.doModal(); 
	}

	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void agentsList(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraBrowseAgent = (Window) Executions.createComponents(
				"/agent.zul", null, map);
		finestraBrowseAgent.doModal(); 
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void captainsList(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraBrowseCaptain = (Window) Executions.createComponents(
				"/captain.zul", null, map);
		finestraBrowseCaptain.doModal(); 
	}

	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void portList(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraBrowsePort = (Window) Executions.createComponents(
				"/port.zul", null, map);
		finestraBrowsePort.doModal(); 
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void brokerList(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraBrowseBroker = (Window) Executions.createComponents(
				"/brokers.zul", null, map);
		finestraBrowseBroker.doModal();
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void paymentsTermsList(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraBrowsePayments = (Window) Executions.createComponents(
				"/payments.zul", null, map);
		finestraBrowsePayments.doModal();
	}

	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void paymentsModeList(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraBrowsePaymentsmode = (Window) Executions.createComponents(
				"/paymentsmode.zul", null, map);
		finestraBrowsePaymentsmode.doModal();
	}

	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void unitMeasureList(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraBrowseUnitmeasure = (Window) Executions.createComponents(
				"/unitmeasure.zul", null, map);
		finestraBrowseUnitmeasure.doModal();
	}

	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void vatArticleList(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraBrowseVatarticle = (Window) Executions.createComponents(
				"/vatarticles.zul", null, map);
		finestraBrowseVatarticle.doModal();
	}

	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void banksList(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraBrowseBanks = (Window) Executions.createComponents(
				"/bank.zul", null, map);
		finestraBrowseBanks.doModal();
	}
	
	// products
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void productsList(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraBrowseProducts = (Window) Executions.createComponents(
				"/products.zul", null, map);
		finestraBrowseProducts.doModal();
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void categoriesList(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraBrowseCategs = (Window) Executions.createComponents(
				"/categories.zul", null, map);
		finestraBrowseCategs.doModal();
	} 

	// Services
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void currencyList(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraBrowseCurrency = (Window) Executions.createComponents(
				"/currency.zul", null, map);
		finestraBrowseCurrency.doModal();
	}
	
	
	// Enquires
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void customersEnquires(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraBrowseReqShipFuelAss = (Window) Executions.createComponents(
				"/reqshipfuelass.zul", null, map);
		finestraBrowseReqShipFuelAss.doModal();
	}

	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void invitationsToQuote(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraBrowseReqShipFuelAss = (Window) Executions.createComponents(
				"/invitationToQuote.zul", null, map);
		finestraBrowseReqShipFuelAss.doModal();
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void quoteToCustomer(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraQuoteToCustomer = (Window) Executions.createComponents(
				"/quoteToCustomer.zul", null, map);
		finestraQuoteToCustomer.doModal();
	}
	

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void acceptanceQuote(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraAcceptanceQuote = (Window) Executions.createComponents(
				"/acceptanceQuote.zul", null, map);
		finestraAcceptanceQuote.doModal();
	}
	
	// Administration
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void invoice(int valore){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		map.put("valore", valore);
		Window finestraAcceptanceQuote = (Window) Executions.createComponents(
				"/invoices/invoices.zul", null, map);
		finestraAcceptanceQuote.doModal();
	}

	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void dayBook(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		Window finestraBrogliaccio = (Window) Executions.createComponents(
				"/invoices/dayBook.zul", null, map);
		finestraBrogliaccio.doModal();
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void customersCard(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		map.put("customers", true);
		Window finestraCards = (Window) Executions.createComponents(
				"/invoices/cust-supp-card.zul", null, map);
		finestraCards.doModal();
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void suppliersCard(){
		HashMap map = new HashMap();
		map.put("seleziona", false);
		map.put("customers", false);
		Window finestraCards = (Window) Executions.createComponents(
				"/invoices/cust-supp-card.zul", null, map);
		finestraCards.doModal();
	}
	
	
}
